/*===============================================
*   文件名称：sqlite3.c
*   创 建 者: Chenqw
*   创建日期：2023年06月30日
*   描    述：
================================================*/
#include <stdio.h>
#include <sqlite3.h>

int callback (void* data, int argc, char**value, char**name)
{
    printf("argc %d\n", argc);
    if(argc == 0)
    {
        printf("NO\n");
    }
    else
    {
        *(int*)data = 1;
    }
    return 0;
}

int main(int argc, char *argv[])
{ 
    sqlite3 * db;
    int ret = sqlite3_open("my.db", &db);
    if(ret != SQLITE_OK)
    {
        printf("open is %s\n", sqlite3_errstr(ret));
        return -1;
    }
    printf("open sqlite is ok\n");
    char* err;
    ret = sqlite3_exec(db, "create table student(name char, id int, age int);", NULL, NULL, &err);
    if(ret != SQLITE_OK)
    {
        printf("create is %s\n", err);
        //return -1;
    }
    ret = sqlite3_exec(db, "insert into student values('kabuda', 01, 18);", NULL, NULL, &err);
    if(ret != SQLITE_OK)
    {
        printf("create is %s\n", err);
        return -1;
    }
    char name[20];
    char sql[200];
    int id;
    scanf("%d%s", &id, name);
    int flag = 0;
    sprintf(sql,"select *from student where name='%s' and id=%d;", name, id);
    ret = sqlite3_exec(db, sql, callback, (void*)&flag, &err);
    if(ret != SQLITE_OK)
    {
        printf("create is %s\n", err);
        return -1;
    }

    if(flag == 0)
    {
        printf("NO\n");
    }
    else
    {
        printf("YES\n");
    }
    return 0;
} 
